import 'package:flutter/material.dart';
import 'package:flutter_plugin_sdk_storekit/channel/store_kit_channel_plugin.dart';
import 'package:flutter_plugin_sdk_storekit_example/my_storekit_callback.dart';

String prouctId = "com.qrtiger.3daystest"; //订阅产品ID
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  //获取订阅数据
  // 1. 配置插件
  StoreKitChannelPlugin().configure(
    callback: MyStoreKitCallback(), // 实现回调接口
  );
  // 2. 获取产品列表，使用正确的方法名
//  final res = await StoreKitChannelPlugin()
//       .getSubscriptionProductList(productIds: ["come.test.0.99", "come.test.month.10"], language: "en-US");
  final res = await StoreKitChannelPlugin().getSubscriptionProductList(productIds: [prouctId], language: "en-US");
  print(res);
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed: () async {
                  try {
                    purchaseSubscription();
                  } catch (e) {
                    print("Error subscribing: $e");
                  }
                },
                child: const Text('订阅'),
              ),
              const SizedBox(height: 20),
              ElevatedButton(
                onPressed: () async {
                  try {
                    restoreSubscribe();
                  } catch (e) {
                    print("Error restoring purchases: $e");
                  }
                },
                child: const Text('恢复订阅'),
              ),
            ],
          ),
        ),
      ),
    );
  }

  //购买订阅
  void purchaseSubscription() async {
    String productId = prouctId; //订阅产品ID
    Map<String, dynamic> reportDic = {
      "user_id": "", //登录用户ID，默认为空
      "user_type": 0, //用户类型: 用户类型: 游客：0，邮箱：1, Google：2, facebook：3, apple：4
      // "ua": Null, //用户代理，是一个特殊字符串头，能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言等
      "idfa": "", //原始 IDFA (仅限 iOS)
      "idfv": "", //原始 IDFV (仅限 iOS)
      "adid": "", //Adjust 设备 ID
      "ad_network": "", //上报Adjust回传的ad_network的对应信息
      "campaign": "", //上报Adjust回传的campaign的对应信息
      "adgroup": "", //上报Adjust回传的adgroup的对应信息
      "creative": "", //上报Adjust回传的creative的对应信息
      "country": "", //可以根据adjust给到的国家两位代码，例如US
      "product_type": "Subscription", //产品类型:Consumable,NonConsumable,Subscription
      // "ss_account_id": Null, //数数接入的账号id,用户的登录ID(如果需要接入数数请务必传此值)
      // "ss_distinct_id": Null, //数数接入的访客id,用户在未登录状态下的ID(如果需要接入数数请务必传此值)
      "unverified_order": 0, //是否验证订单,0:默认验证，1：不验证，如果已经调用过iap支付订单验证接口，此字段设置为1不验证即可，如果没有使用iap订单验证接口，建议采用0进行服务端验证
    };
    final result = await StoreKitChannelPlugin().purchaseSubscription(productId, reportDic);
    if (result.$1) {
      //订阅成功
    } else {
      //订阅失败
      debugPrint(result.$2);
    }
  }

  //恢复订阅
  void restoreSubscribe() async {
    final result = await StoreKitChannelPlugin().restoreSubscription();
    if (result.$1) {
      //恢复订阅成功
    } else {
      //恢复订阅失败
      debugPrint(result.$2);
    }
  }
}
